<template>
  <a-modal
    :title="'修改商户 ' + name"
    :visible="visible"
    @ok="onSubmit"
    @cancel="visible = false"
  >
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 14 }"
    >
      <a-form-model-item ref="fee_ratio" label="买币手续费率" prop="fee_ratio">
        <a-input-number
          v-model="form.fee_ratio"
          :step="0.001"
          :min="0"
          :max="max_fee"
        />
      </a-form-model-item>

      <a-form-model-item ref="tip" label="下发手续费" prop="tip">
        <a-input-number v-model="form.tip" :min="0" :max="max_tip" />
      </a-form-model-item>

      <a-form-model-item label="手机号" prop="mobile">
        <a-input v-model="form.mobile" />
      </a-form-model-item>

      <a-form-model-item label="登录密码" prop="password">
        <a-input v-model="form.password" type="password" />
      </a-form-model-item>

      <a-form-model-item label="启用/禁用" prop="enable">
        <a-switch v-model="form.enable" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      visible: false,
      id: null,
      name: '',
      // code: '',
      max_fee: 0,
      max_tip: 0,
      form: {
        password: '',
        fee_ratio: 0,
        tip: 0,
        enable: false,
      },
      rules: {
        fee_ratio: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
        tip: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  computed: {
    ...mapGetters('account', ['sys']),
  },
  created() {
    this.max_fee = parseFloat(this.sys.global_fee_ratio)
    this.max_tip = parseFloat(this.sys.global_tip)
  },
  methods: {
    show(row) {
      this.id = row.id
      this.name = row.username
      // this.code = row.code
      this.form = {
        password: '',
        mobile: row.mobile,
        fee_ratio: row.fee_ratio,
        tip: row.tip,
        enable: row.enable,
      }
      this.visible = true
    },
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //   console.log(this.form)
          this.axios.put(`/a/sellers/${this.id}`, this.form).then((res) => {
            this.visible = false
            this.$message.success('修改成功')
            this.$emit('refresh')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
